﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Card.DataBase
{
    public class BaseDAL
    {
        public static string ConnectionString
        {
            get
            {
                return DBManager.ConnectionString;
            }
        }

        public static object DateTimeToDBObject(DateTime date)
        {
            if (DateTime.Compare(date, DateTime.MaxValue) == 0 ||
                DateTime.Compare(date, DateTime.MinValue) == 0
                )
            {
                return DBNull.Value;
            }
            return date;
        }

        public DataTable Pagination(Pagination pagination)
        {
            string cmdText = "[dbo].[usp_Pagination]";

            SqlParameter[] cmdParms = new SqlParameter[7];
            int i = -1;

            i++;
            cmdParms[i] = new SqlParameter("@TableName", SqlDbType.NVarChar);
            cmdParms[i].Direction = ParameterDirection.Input;
            cmdParms[i].Value = pagination.TableName;

            i++;
            cmdParms[i] = new SqlParameter("@ShowColumn", SqlDbType.NVarChar);
            cmdParms[i].Direction = ParameterDirection.Input;
            cmdParms[i].Value = pagination.ShowColumns;

            i++;
            cmdParms[i] = new SqlParameter("@Condition", SqlDbType.NVarChar);
            cmdParms[i].Direction = ParameterDirection.Input;
            cmdParms[i].Value = pagination.Condition;


            i++;
            cmdParms[i] = new SqlParameter("@orderby", SqlDbType.NVarChar);
            cmdParms[i].Direction = ParameterDirection.Input;
            cmdParms[i].Value = pagination.OrderBy;

            i++;
            cmdParms[i] = new SqlParameter("@PageNumber", SqlDbType.Int);
            cmdParms[i].Direction = ParameterDirection.Input;
            cmdParms[i].Value = pagination.PageNumber <= 0 ? 1 : pagination.PageNumber;

            i++;
            cmdParms[i] = new SqlParameter("@PageSize", SqlDbType.Int);
            cmdParms[i].Direction = ParameterDirection.Input;
            cmdParms[i].Value = pagination.PageSize;

            i++;
            cmdParms[i] = new SqlParameter("@RecordCount", SqlDbType.Int);
            cmdParms[i].Direction = ParameterDirection.Output;

            DataTable dtResult = SqlHelper.ExecuteDataset(ConnectionString, CommandType.StoredProcedure, cmdText, cmdParms).Tables[0];
            pagination.RecordCount = int.Parse(cmdParms[i].Value.ToString());

            return dtResult;
        }
    }
}
